import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class QuackCounterTest {
	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

	@Before
	public void setUpStreams() {
	    System.setOut(new PrintStream(outContent));
	    System.setErr(new PrintStream(errContent));
	}

	@After
	public void cleanUpStreams() {
	    System.setOut(null);
	    System.setErr(null);
	}
	
	@Test
	public void testQuack() {
		Quackable counter = new QuackCounter(new RedheadDuck());
		counter.quack();
		String out = outContent.toString();
		assertEquals("Quack\n", out);
	}

	@Test
	public void testGetQuacks() {
		QuackCounter.resetQuacks();
		assertEquals(0, QuackCounter.getQuacks());
		
		Quackable redheadDuck = new QuackCounter(new RedheadDuck());	
		redheadDuck.quack();
		assertEquals(1, QuackCounter.getQuacks());
		
		Quackable goose = new QuackCounter(new GooseAdapter(new Goose()));
		assertEquals(1, QuackCounter.getQuacks());
		goose.quack();
		assertEquals(2, QuackCounter.getQuacks());
		
		QuackCounter.resetQuacks();
		assertEquals(0, QuackCounter.getQuacks());		
		
	}

}
